package com.example.showdemo.model;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.example.showdemo.viewmodel.IViewModel;

public class HandleModel implements IModel {

    private IViewModel viewModel = null;
    private Handler handler = new Handler(Looper.getMainLooper());

    public void handleData(String data) {
        if (TextUtils.isEmpty(data)) {
            return;
        }
        handler.removeCallbacksAndMessages(null);
        // 延迟来模拟网络或者磁盘操作
        handler.postDelayed(new Runnable() {
            @Override
            // 数据处理完成通知 ViewModel
            public void run() {
                viewModel.dataHandled("handled data: $data");
            }
        }, 3000);
    }

    public void clearData() {
        handler.removeCallbacksAndMessages(null);
        // 数据清理完成通知 ViewModel
        viewModel.dataCleared();
    }

    public void setViewModel(IViewModel viewModel) {
        this.viewModel = viewModel;
    }
}
